package config

import (
	"time"

	"gitee.com/xgotool/backend-framework-template/model"
	"gitee.com/xiaoyutab/xgotool/individual/xqueue"
	"gitee.com/xiaoyutab/xgotool/xstring"
)

// 自动初始化数据表信息
func autoDatabases() {
	if db, err := roots.DB(); err == nil {
		menus := model.Menu{}
		// 删除菜单表，再行创建
		db.Exec("DROP TABLE IF EXISTS " + menus.TableName())
	}
	roots.AutoMigrate(
		&model.User{}, &model.UserLog{}, // 用户表
		&model.Wallet{}, &model.WalletLog{}, // 用户钱包表
		&model.Order{}, &model.OrderAddress{}, &model.OrderItems{}, &model.OrderStatusLog{}, // 订单表
		&model.Address{},             // 用户地址表
		&model.Role{}, &model.Menu{}, // 角色/菜单表
		&model.Configure{}, // 系统配置项
	)
	// 初始化用户添加
	mod := model.User{}
	// 此处使用删除操作，强行修改用户为空，致使强制初始化用户信息修改为强行添加
	roots.Model(mod.TableName()).Where("id", 1).Delete(&mod)
	// roots.Table(mod.TableName()).First(&mod, "id", 1)
	if mod.Id == 0 {
		// 添加超级用户
		mod.Id = 1
		mod.UserName = "admin"
		rand := xstring.Random(6)
		mod.Password = xstring.MD5("admin"+rand) + rand
		mod.CreatedAt = time.Now().Unix()
		mod.Status = 1
		roots.Table(mod.TableName()).Create(&mod)
		// 因为此处使用service的话会造成循环依赖，所以这里直接使用model进行添加，手动追加hook事件
		xqueue.Set("hook.user.create", mod)

		// 添加菜单
		menus := []model.Menu{
			// 主菜单
			{
				Title:      "系统管理",
				Icon:       "Setting",
				Path:       "sys",
				Permission: "system",
				Childs: []model.Menu{
					{
						Title:      "菜单管理",
						Icon:       "Menu",
						Component:  "system/menu/index",
						Path:       "menu",
						Permission: "sys:menu:list",
						Childs: []model.Menu{
							{Title: "新增菜单", Permission: "system:menu:add"},
							{Title: "修改菜单", Permission: "system:menu:edit"},
						},
					},
					{
						Title:      "角色管理",
						Icon:       "User",
						Component:  "system/role/index",
						Path:       "role",
						Permission: "sys:role:list",
						Childs: []model.Menu{
							{Title: "搜索角色", Permission: "system:role:list"},
							{Title: "添加角色", Permission: "system:role:add"},
							{Title: "修改角色", Permission: "system:role:edit"},
							{Title: "删除角色", Permission: "system:role:delete"},
						},
					},
					{
						Title:      "用户管理",
						Icon:       "User",
						Component:  "system/user/index",
						Path:       "user",
						Permission: "sys:user:list",
						Childs: []model.Menu{
							{Title: "搜索用户", Permission: "system:user:list"},
							{Title: "添加用户", Permission: "system:user:add"},
							{Title: "修改用户", Permission: "system:user:edit"},
						},
					},
					{
						Title:      "系统配置",
						Icon:       "Setting",
						Component:  "system/configure/list",
						Path:       "configure",
						Permission: "sys:configure:list",
						Childs: []model.Menu{
							{Title: "配置保存", Permission: "sys:configure:save"},
						},
					},
				},
			},
			// 数据管理
			{
				Title:      "用户数据",
				Icon:       "User",
				Path:       "users",
				Permission: "users",
				Childs: []model.Menu{
					{
						Title:      "地址管理",
						Icon:       "AddLocation",
						Component:  "users/address/list",
						Path:       "address",
						Permission: "users:address:list",
						Childs: []model.Menu{
							{Title: "地址搜索", Permission: "users:address:search"},
							{Title: "地址添加", Permission: "users:address:create"},
							{Title: "地址修改", Permission: "users:address:update"},
							{Title: "地址删除", Permission: "users:address:delete"},
						},
					},
					{
						Title:      "钱包管理",
						Icon:       "Wallet",
						Path:       "account",
						Component:  "users/account/list",
						Permission: "users:account:list",
						Childs: []model.Menu{
							{Title: "钱包搜索", Permission: "users:account:search"},
							{Title: "钱包充值", Permission: "users:account:recharge"},
							{Title: "钱包扣除", Permission: "users:account:withdrawal"},
							{Title: "钱包明细", Permission: "users:account:detail"},
						},
					},
				},
			},
		}
		for i := 0; i < len(menus); i++ {
			menus[i].Status = 1
			menus[i].MenuType = 1
			menus[i].CreateTime = time.Now().Unix()
			menus[i].UpdateTime = time.Now().Unix()
			roots.Table(menus[i].TableName()).Create(&menus[i])
			if len(menus[i].Childs) > 0 {
				// 二级菜单添加
				for j := 0; j < len(menus[i].Childs); j++ {
					menus[i].Childs[j].MenuType = 2 // 菜单
					menus[i].Childs[j].ParentId = menus[i].Id
					menus[i].Childs[j].Status = 1
					menus[i].Childs[j].CreateTime = time.Now().Unix()
					menus[i].Childs[j].UpdateTime = time.Now().Unix()
					roots.Table(menus[i].Childs[j].TableName()).Create(&menus[i].Childs[j])
					if len(menus[i].Childs[j].Childs) > 0 {
						// 三级菜单添加
						for k := 0; k < len(menus[i].Childs[j].Childs); k++ {
							menus[i].Childs[j].Childs[k].MenuType = 3 // 按钮
							menus[i].Childs[j].Childs[k].ParentId = menus[i].Childs[j].Id
							menus[i].Childs[j].Childs[k].Status = 1
							menus[i].Childs[j].Childs[k].CreateTime = time.Now().Unix()
							menus[i].Childs[j].Childs[k].UpdateTime = time.Now().Unix()
							roots.Table(menus[i].Childs[j].Childs[k].TableName()).Create(&menus[i].Childs[j].Childs[k])
						}
					}
				}
			}
		}
	}
}
